/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年3月18日         luwei
 */

package com.coocaa.app.gamecenter.pages.mygame;

import android.content.Intent;
import android.os.Bundle;
import android.os.RemoteException;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.coocaa.app.gamecenter.framework.GameCenterActivity;
import com.coocaa.app.gamecenter.framework.ui.LoadMoreGridView.LoadMoreDataCallback;
import com.coocaa.x.app.framework.app.LiteAppApplication;
import com.coocaa.x.app.framework.app.LiteAppApplication.ContentAppChangedReceiver.ContentAppChangedReceiverListener;
import com.coocaa.x.framework.utils.LiteURI;
import com.coocaa.x.service.litecontent.LiteContentServiceConnecter;
import com.coocaa.x.service.litecontent.data.apps.com_coocaa_app.AppData;
import com.coocaa.x.service.litecontent.data.apps.com_coocaa_app.RP_Installed_Apps;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.data.SkyListAppData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyGameActivity extends GameCenterActivity implements LoadMoreDataCallback, ContentAppChangedReceiverListener {
    private MyGameLayout mLayout = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        showloading();
        LiteAppApplication.addContentAppChangedReceiverListener(this);
    }

    @Override
    protected Map<String, String> getPageStartParams() {
        HashMap<String,String> map = new HashMap<String,String>();
        return map;
    }

    @Override
    protected View createContentView() {
        mLayout = new MyGameLayout(this);
        mLayout.setLoadMoreCallback(this);
        mLayout.hideFocusView();
        // mLayout.loadData();
        return mLayout;
    }

    @Override
    protected void loadData() throws Exception {
        updateLocalGames();
        hideloading();
    }

    private void updateLocalGames() {
        LiteURI uri = LiteURI.create("apps://com.coocaa.app/installed_apps");
        uri.addParams("filter", "game");
        String result;
        try {
            result = LiteContentServiceConnecter.getService().call(uri.toString());
            if (result != null && !result.equals("")) {
                RP_Installed_Apps data = JSONObject.parseObject(result, RP_Installed_Apps.class);
                if (data.error_code == 0 && data.resource != null
                        && data.resource.total_count > 0) {
                    mLayout.refreshUI(generateAppList(data.resource.resource_items));
                } else {
                    mLayout.refreshUI(new ArrayList<SkyListAppData>());
                    setLoadDataErrorMsg(getString(R.string.no_my_game));
                }
            }
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    protected void updateContentView() {
        updateTitle(getString(R.string.gc_my_game));
    }

    @Override
    public List<SkyListAppData> loadMordData(int page) {
        return null;
    }

    public List<SkyListAppData> generateAppList(List<AppData> appInfos) {
        List<SkyListAppData> listAppDatas = new ArrayList<SkyListAppData>();
        mLayout.setTotalNum(appInfos.size());
        if (appInfos.size() == 0) {
            setLoadDataErrorMsg(getString(R.string.no_my_game));
            mLayout.hideFocusView();
        } else {
            for (int i = 0; i < appInfos.size(); i++) {
                SkyListAppData data = new SkyListAppData("", appInfos.get(i).info.appName, "", "",
                        appInfos.get(i).info.pkgName);
                data.setMainActivity(appInfos.get(i).info.mainActivity);
                listAppDatas.add(data);
            }
            mLayout.showFocusView();
        }
        return listAppDatas;
    }

    @Override
    protected void onDestroy() {
        mLayout.destory();
        LiteAppApplication.removeContentAppChangedReceiverListener(this);
        super.onDestroy();
    }

    @Override
    public void onAction(Intent i) {
        post(new Runnable() {
            @Override
            public void run() {
                updateLocalGames();
            }
        });
    }

    @Override
    public String getName() {
        return "酷游吧我的游戏页";
    }
}
